local jLootz = jLootz
local lib = jLootz:NewModule("Frame")

function lib:EnableDefaultInterface(enable)
	local LootFrame = LootFrame
	StaticPopup_Hide("LOOT_BIND")
	HideUIPanel(LootFrame)
	CloseLoot(true) -- fake uiFailed here
	if not enable then
		if not jLootz_DONT_DISABLE_BASE_UI then
			LootFrame:UnregisterEvent("LOOT_OPENED")
			LootFrame:UnregisterEvent("LOOT_SLOT_CLEARED")
			LootFrame:UnregisterEvent("LOOT_CLOSED")
			UIParent:UnregisterEvent("LOOT_BIND_CONFIRM")
		end
	else
		LootFrame:RegisterEvent("LOOT_OPENED")
		LootFrame:RegisterEvent("LOOT_SLOT_CLEARED")
		LootFrame:RegisterEvent("LOOT_CLOSED")
		UIParent:RegisterEvent("LOOT_BIND_CONFIRM")
	end
end

function lib:LOOT_OPENED(autoLoot)
	self.frame:Show()
end

function lib:LOOT_CLOSED()
	self:HideConfirm()
	self.frame:Hide()
end

function lib:LOOT_SLOT_CLEARED()
	if self.frame:IsShown() then
		self:Update()
	end
end

function lib:LOOT_BIND_CONFIRM(slot)
	local buttons = self.buttons
	for i = 1, (buttons.count or 0) do
		local button = buttons[i]
		if button.slot == slot then
			self:ShowConfirm(button, slot)
		end
	end
	buttons.count = nil
end
-- /run jLootz:GetModule("Frame"):LOOT_BIND_CONFIRM(1)

function lib:OnInitialize()
	self.db = jLootz.db:RegisterNamespace("Frame")
	self.buttons = {count = 0}
	self.OnInitialize = nil
end

function lib:OnEnable()
	self:EnableDefaultInterface(false)
	local pos = self.db.profile.frameposition
	self.frame:ClearAllPoints()
	if pos then
		self.frame:SetPoint("TOPLEFT", UIParent, pos.x, pos.y)
	else
		self.frame:SetPoint("CENTER")
	end
end

function lib:OnDisable()
	self:EnableDefaultInterface(true)
end

function lib:Update()
	self:ClearLootFrames()
	for slot = 1, GetNumLootItems() do
		self:FillLootFrame(self:GetLootFrame(), slot, GetLootSlotInfo(slot))
	end
	self:ResizeFrame(GetNumLootItems())
end

function lib:ReportLoots()
	local channel
	if GetNumRaidMembers() > 0 then
		channel = "RAID"
	elseif GetNumPartyMembers() > 0 then
		channel = "PARTY"
	end
	if not channel then return end
	local msg = UnitName("target")..": "
	local slot = 1
	while slot <= GetNumLootItems() do
		local link = GetLootSlotLink(slot) 
		slot = slot + 1
		if msg:len() + link:len() <= 250 then
			msg = msg..link
		else
			SendChatMessage(msg, channel)
			msg = link
		end
	end
	SendChatMessage(msg, channel)
end

function lib:ResizeFrame(lines)
	local frame = self.frame
	frame:ClearAllPoints()
	if self.db.left and self.db.top then
		frame:SetPoint("TOPLEFT", self.db.left, self.db.top - GetScreenHeight())
	else
		frame:SetPoint("CENTER")
	end
	frame:SetWidth(232)
	if lines > 0 then
		frame:SetHeight(lines*40+16)
	else
		frame:SetHeight(36)
	end
end

function lib:SaveFramePosition(left, top)
	self.db.left = left
	self.db.top = top
end

function lib:SetMasterLootTarget(anchor, slot)
	local master = jLootz:GetModule("Master", true)
	if master then
		master:SetTarget(anchor, slot)
	else
		local _, name, _, quality = GetLootSlotInfo(slot)
		LootFrame.selectedItemName = name
		LootFrame.selectedQuality = quality
		LootFrame.selectedLootButton = self
		LootFrame.selectedSlot = slot
	end
end
